<?php


namespace App\Services;


use App\Job\Job;
use Hyperf\AsyncQueue\Message;
use Illuminate\Support\Facades\Redis;

require_once "Message.php";

class QueueService
{
    public function produce(Job $job, int $delay = 0) : bool
    {
        $redis = Redis::connection('queue')->client();
        $message = new Message($job);
        $data = serialize($message);

        if ($delay) {
            return $redis->zAdd('queue:delayed', time() + mt_rand(1, $delay), $data) > 0;
        } else {
            return $redis->lPush('queue:waiting', $data);
        }
    }
}
